/*
 * TableModelStandar.java
 *
 * Created on 8 de septiembre de 2006, 11:41 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.ik.Reposteria.SMP.Ventanas.Modelos;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author camacho
 */
public class TableModelStandar extends AbstractTableModel {
    
    /**
     * Creates a new instance of TableModelStandar
     */
        public boolean cellEditable = false;
	public ArrayList dataArray;
        public int[] columneditable=null;

	/** The <code>ArrayList</code> of column identifiers. */
	public ArrayList columnIdentifiers;
        
    public TableModelStandar(){
        
    }    
    public TableModelStandar(ArrayList data,ArrayList column) {
        dataArray = data;
        columnIdentifiers = column;
        //columneditable = editables;
    }
    
    public TableModelStandar(ArrayList column) {
        dataArray = new ArrayList(4);
        columnIdentifiers = column;
        //columneditable = editables;
    }
    
    public Object getRow(int rowIndex){           
           return dataArray.get(rowIndex);
           
      }
    
        
    public void addRow(Object row){
        dataArray.add(row);
        
        fireTableDataChanged();
       }
    
      public void setEditableColumns(int[] editables){
      columneditable = editables;
      
      }

         public void setData(ArrayList data){
        dataArray = data;
        fireTableDataChanged();
      
      }
    
    
    //***********************PARTE COMUN**********************************
    
        public int getColumnCount() {
		return columnIdentifiers.size();
	}


	public void removeRow(int row) {
		dataArray.remove(row);
		fireTableRowsDeleted(row, row);
	}
        
	/**
	 * Returns the number of rows in this data table.
	 * 
	 * @return the number of rows in the model
	 */
	public int getRowCount() {
            if(dataArray != null)
		return dataArray.size();
            return 0;
	}

              
	
    	public String getColumnName(int column) {
		return (String)columnIdentifiers.get(column);
	}

    
	public boolean isCellEditable(int row, int column) {
		if(columneditable != null){
                    if(columneditable.length>0){
                        for(int i = 0;i<columneditable.length;i++){
                          if(columneditable[i]==column){
                            return true;
                          }
                    
                      }
                                                       
                    }else{
                        return cellEditable;
                    }
               }
                return cellEditable;
	}

	
    
    public Class getColumnClass(int columnIndex) {
        try {
            if (getRowCount() == 0) { return Object.class; }                   
        }catch(NullPointerException ex) {}
        try {
            return getValueAt(0, columnIndex).getClass(); 
        } catch(NullPointerException ex) {}
        return this.getClass();
    }

	
	public Object getValueAt(int rowIndex, int columnIndex) {
		ArrayList rowArray = (ArrayList) dataArray.get(rowIndex);
		return rowArray.get(columnIndex);
	}
        
	
    
	public void setValueAt(Object aValue, int row, int column) {
		ArrayList rowArray = (ArrayList) dataArray.get(row);
		rowArray.set(column, aValue);
		fireTableCellUpdated(row, column);
	}

	protected ArrayList getDataArray() {
		return dataArray;
	}
	
	/**
	 * @return A cloned copy of <code>dataArray</code>.
	 */
	public ArrayList getData() {
		return new ArrayList((ArrayList) getDataArray().clone());
	}   // getData()
	
	public ArrayList getColumnNames() {
		return (ArrayList)columnIdentifiers.clone();
	}
	public void insertRow(int row, ArrayList rowData) {
		dataArray.add(row, rowData);
		fireTableRowsInserted(row, row);
	} 
	
  //****************************************************************************  

 
}
